package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import javazoom.jl.decoder.JavaLayerException;

public class PlayPanel extends JPanel {
	final int ROW = 30;
	final int COL = 30;

	// Design GUI
	ImagePanel imgbg = new ImagePanel(
			new ImageIcon("images/matrix.png").getImage());
	ImageIcon checkOIc = new ImageIcon("images/checkO.png");
	ImageIcon checkXIc = new ImageIcon("images/checkX.png");
	JButton[][] table = new JButton[30][30];
	int[][] values = new int[30][30];
	String value;
	public final int OPLAYER = 1;
	public final int XPLAYER = 2;
	public int firstPlay;

	// Audio
	Audio winSound = new Audio("sounds/win.mp3");
	Audio clickSound = new Audio("sounds/click.mp3");

	// TCP Components
	public ServerSocket hostServer = null;
	public Socket socket = null;
	public BufferedReader in = null;
	public PrintWriter out = null;

	// /////////////////////////////////////////////////////////////
	public PlayPanel() {
		imgbg.setLayout(new GridLayout(30, 30));
		for (int i = 0; i < ROW; i++)
			for (int j = 0; j < COL; j++) {
				final int row = i;
				final int col = j;
				table[i][j] = new JButton();
				table[i][j].setPreferredSize(new Dimension(20, 20));
				table[i][j].setContentAreaFilled(false);
				table[i][j].setBorderPainted(false);
				table[i][j].setFocusPainted(false);
				table[i][j].addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						table[row][col].setIcon(setIcon(firstPlay));
						values[row][col] = firstPlay;
						if (checkWinner(row, col))
							System.out.println("You win");else
								System.out.println("You lose");
					}
				});
				imgbg.add(table[i][j]);
				// table[i][j].g
			}
		setLayout(new GridLayout(1, 1));
		add(imgbg);
	}

	public ImageIcon setIcon(int firstPlay) {
		try {
			clickSound.play();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JavaLayerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (firstPlay == XPLAYER)
			return new ImageIcon("images/checkX.png");
		else
			return new ImageIcon("images/checkO.png");
	}

	public boolean checkWinner(int currentRow, int currentCol) {
		if (checkWinnerVertical(currentCol) == true
				|| checkWinnerHorizontal(currentRow) == true
				|| checkWinnerCrossLeft(currentRow, currentCol) == true
				|| checkWinnerCrossRight(currentRow, currentCol) == true)
			return true;
		return false;
	}

	public boolean checkWinnerHorizontal(int currentRow) {
		int count = 0;
		for (int i = 0; i < COL; i++) {
			if (values[currentRow][i] == firstPlay) {
				count++;
			} else {
				count = 0;
			}
			if (count == 5)
				return true;
		}
		return false;
	}

	public boolean checkWinnerVertical(int currentCol) {
		int count = 0;
		for (int j = 0; j < ROW; j++) {
			if (values[j][currentCol] == firstPlay)
				count++;
			else
				count = 0;
			if (count == 5)
				return true;
		}
		return false;
	}

	public boolean checkWinnerCrossLeft(int currentRow, int currentCol) {
		int count = 0;
		while (currentRow != 0 && currentCol != 0) {
			currentRow--;
			currentCol--;
		}
		while (currentRow < ROW - 1 && currentCol < COL - 1) {
			if (values[++currentRow][++currentCol] == firstPlay)
				count++;
			else
				count = 0;
			if (count == 5)
				return true;
		}
		return false;
	}

	public boolean checkWinnerCrossRight(int currentRow, int currentCol) {
		int count = 0;
		while (currentRow != 0 && currentCol < COL - 1) {
			currentRow--;
			currentCol++;
		}
		while (currentRow < ROW - 1 && currentCol != 0) {
			if (values[currentRow++][currentCol--] == firstPlay)
				count++;
			else
				count = 0;
			if (count == 5)
				return true;
		}
		return false;
	}

	public void disableAButton(int i, int j, boolean value) {
		if (value)
			table[i][j].setEnabled(false);
		else
			table[i][j].setEnabled(true);
	}

	public void disableAll(boolean value) {
		for (int i = 0; i < ROW; i++)
			for (int j = 0; j < COL; j++)
				disableAButton(i, j, value);
	}
}
